using System;
using System.Collections.Generic;
using System.Text;
using Ent.Voice.Nomination;
using Ent.Voice.Organisation;
using System.Collections;
using Ent.SqlDB;
using System.Data;
namespace Ent.Voice.Show
{
    public enum Track { None = 0, IP = 1, Email = 2, All = 3 }
    public enum EventAccess { Private = 1, Public = 2 }
    public enum EventParticipation { Default = 1, Limited = 2 }
    public enum EventAccessPublic { None = 0, PublicViewable = 1, PublicParticipable = 2, All = 3 }
    public enum EventStatus { Closed = 0, Started = 1, YetToStart = 2, Suspended = 3 }
    public enum EventResponse { Error, EventCreated, EventUpdated }
    public enum EventNomCurrency { Rupee = 1, Dollar = 2 }
    public enum EventMandate { None = 0, Name = 1, EMail = 2, All = 3}
    public enum EventFileType { None = 0, Image = 1, Audio = 2, Video = 3, Links = 4, Text = 5 }
    public class EventProperty
    {
        public const string EventId = "EventId";
        public const string Catagory = "Catagory";
        public const string Title = "Title";
        public const string Description = "Description";
        public const string Access = "Access";
        public const string AccessPublic = "AccessPublic";
        public const string AccessCode = "AccessCode";
        public const string Fixed = "Fixed";
        public const string Count = "Count";
        public const string StartDate = "StartDate";
        public const string DueDate = "DueDate";
        public const string MaxVoteCount = "MaxVoteCount";
        public const string VoterTracking = "VoterTracking";
        public const string Mandatory = "Mandatory";
        public const string VoteStDate = "VoteStDate";
        public const string NomineeTracking = "NomineeTracking";
        public const string Participation = "Participation";
        public const string ParticipationStDate = "ParticipationStDate";
        public const string MaxPart = "MaxPart";
        public const string MaxPartLimit = "MaxPartLimit";
        public const string NomAmount = "NomAmount";
        public const string NCurrency = "NCurrency";
        public const string Upload = "Upload";
        public const string FileType = "FileType";
        public const string Sponcership = "Sponcership";
        public const string SponcerStData = "SponcerStData";
        public const string MinSponsor = "MinSponsor";
        public const string SCurrency = "SCurrency";
        public const string SkinId = "SkinId";
        public const string Invitees = "Invitees";
        public const string Criteria = "Criteria";
    }
    public class Event
    {
        public static readonly DateTime MinDate = new DateTime(2001, 1, 1);
        public static readonly DateTime MaxDate = new DateTime(3001, 1, 1);
        public int Id;
        public string Name;
        public string Description;
        public int CatagoryId;
        public int Count;
        public int Votes;
        public Entry[] Entries;
        public EventStatus Status;
        public DateTime StartDate;
        public DateTime EndDate;
        public int FeeAmount;
        public EventNomCurrency Currency;
        public DateTime NominationStartDate;
        public EventStatus NominationStatus;
        public DateTime SponsorStartDate;
        public EventStatus SponsorStatus;
        public EventAccess Access;
        public EventAccessPublic AccessPublic;
        public EventWinner[] Winners;
        public string AccessCode;
        public User Organiser;
        public User[] Sponsorers;
        public VoteCriteria VoteCriteria;
        public Track VoterTrack;
        public Track NominationTrack;
        public int MaxNominationCount;
        public bool AllowFileUpload;
        public List<EventFileType> UploadFileTypes;
        public bool AllowNomination;
        public bool AllowSponsorship;
        public int MinSponsorAmt;
        public DateTime VoteStartDate;
        public int SkinId;
        public int MaxVoteCount;
        public EventMandate MandateField;
        public string Invitees;
        public Dictionary<string, string> Criteria;

        public static Entry[] GetEntriesWithinRange(int eventId, int startRange, int count, bool includeSuspended, string viewId, ref int total)
        {
            return GetEntriesWithinRange(eventId, startRange, count, includeSuspended, viewId, ref total, OrderBy.Points);
        }
        public static Entry[] GetEntriesWithinRange(int eventId, int startRange, int count, bool includeSuspended, string viewId, ref int total, OrderBy orderby)
        {
            if (count < 0)
                throw new Exception("Count should be a positive integer.");
            if (count == 0)
                return new Entry[0];
            if (startRange <= 0)
                throw new Exception("Start range should be a positive integer.");
            Entry[] retValue = Entry.Search(EntryFilter.Event, eventId.ToString(), "", startRange, count, orderby.ToString(), includeSuspended, viewId, ref total);
            return retValue;

        }
        public static VoteCriteria GetVoteCriteria(int eventId)
        {
            VoteCriteria retValue = new VoteCriteria();
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Id", eventId);
            pars.Add("Type", "vote");

            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("Mandate", new VSqlDbType(SqlDbType.VarChar, 25));
            ReaderResponse rr = Execute.SPReader("Voice_GetCriteria", pars, outpars);
            retValue.MandateField = Convert.ToString(rr.OutParams["Mandate"]);

            if (rr.Table.Columns.Contains("CName"))
            {
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    DataRow dr = rr.Table.Rows[i];
                    switch (Convert.ToString(dr["CName"]).ToLower())
                    { 
                        case "age":
                            retValue.Age = Convert.ToString(dr["CValue"]);
                            break;
                        case "country":
                            retValue.Country = Convert.ToString(dr["CValue"]);
                            break;
                    }
                }
            }
            return retValue;
        }
        public static Event GetEvent(int eventId, int start, int count, bool includeSuspended,string viewId, ref int total)
        {
            Event retValue = new Event();
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Id", eventId);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("FileTypes", new VSqlDbType(SqlDbType.VarChar, 100));
            outpars.Add("Criteria", new VSqlDbType(SqlDbType.VarChar, 200));
            ReaderResponse rr = Execute.SPReader("Voice_GetEventDetails", pars, outpars);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                retValue = SetEvent(rr.Table.Rows[0]);
                if (retValue != null)
                {
                    retValue.Entries = Event.GetEntriesWithinRange(eventId, start, count, includeSuspended, viewId, ref total);
                }
                string[] fTypes = rr.OutParams["FileTypes"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                retValue.UploadFileTypes = new List<EventFileType>();
                foreach (string ft in fTypes)
                {
                    retValue.UploadFileTypes.Add((EventFileType)Convert.ToInt32(ft));
                }

                string[] cTypes = rr.OutParams["Criteria"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                retValue.Criteria = new Dictionary<string, string>();
                foreach (string ct in cTypes)
                {
                    string[] cKV = ct.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (cKV.Length < 2)
                        continue;
                    retValue.Criteria.Add(cKV[0], cKV[1]);
                }
                return retValue; 
            }
            else
                return null;
                   
        }
        private static Event SetEvent(DataRow dr)
        {
            Event retValue = new Event();
            if (dr.Table.Columns.Contains("Id")) retValue.Id = dr["Id"] != System.DBNull.Value ? Convert.ToInt32(dr["Id"]) : 0;
            if (dr.Table.Columns.Contains("Name")) retValue.Name = dr["Name"] != System.DBNull.Value ? Convert.ToString(dr["Name"]) : "";
            if (dr.Table.Columns.Contains("Description")) retValue.Description = dr["Description"] != System.DBNull.Value ? Convert.ToString(dr["Description"]) : "";
            if (dr.Table.Columns.Contains("CatagoryId")) retValue.CatagoryId = dr["CatagoryId"] != System.DBNull.Value ? Convert.ToInt32(dr["CatagoryId"]) : 0;
            if (dr.Table.Columns.Contains("Count")) retValue.Count = dr["Count"] != System.DBNull.Value ? Convert.ToInt32(dr["Count"]) : 0;
            if (dr.Table.Columns.Contains("Votes")) retValue.Votes = dr["Votes"] != System.DBNull.Value ? Convert.ToInt32(dr["Votes"]) : 0;
            if (dr.Table.Columns.Contains("Status")) retValue.Status = dr["Status"] != System.DBNull.Value ? (EventStatus)Enum.Parse(typeof(EventStatus), Convert.ToString(dr["Status"]), true) : EventStatus.Closed;
            if (dr.Table.Columns.Contains("EventStartDate")) retValue.StartDate = dr["EventStartDate"] != System.DBNull.Value ? Convert.ToDateTime(dr["EventStartDate"]) : DateTime.MinValue;
            if (dr.Table.Columns.Contains("EventEndDate")) retValue.EndDate = dr["EventEndDate"] != System.DBNull.Value ? Convert.ToDateTime(dr["EventEndDate"]) : DateTime.MinValue;
            if (dr.Table.Columns.Contains("FeeAmount")) retValue.FeeAmount = dr["FeeAmount"] != System.DBNull.Value ? Convert.ToInt32(dr["FeeAmount"]) : 0;
            if (dr.Table.Columns.Contains("Currency")) retValue.Currency = dr["Currency"] != System.DBNull.Value ? (EventNomCurrency)Enum.Parse(typeof(EventNomCurrency), Convert.ToString(dr["Currency"]), true) : EventNomCurrency.Rupee;
            if (dr.Table.Columns.Contains("Access")) retValue.Access = dr["Access"] != System.DBNull.Value ? (EventAccess)Enum.Parse(typeof(EventAccess), Convert.ToString(dr["Access"]), true) : EventAccess.Private;
            if (dr.Table.Columns.Contains("AccessPublic")) retValue.AccessPublic = dr["AccessPublic"] != System.DBNull.Value ? (EventAccessPublic)Enum.Parse(typeof(EventAccessPublic), Convert.ToString(dr["AccessPublic"]), true) : EventAccessPublic.None;
            if (dr.Table.Columns.Contains("AccessCode")) retValue.AccessCode = dr["AccessCode"] != System.DBNull.Value ? Convert.ToString(dr["AccessCode"]) : "";
            if (dr.Table.Columns.Contains("NominationStartDate")) retValue.NominationStartDate = dr["NominationStartDate"] != System.DBNull.Value ? Convert.ToDateTime(dr["NominationStartDate"]) : DateTime.MinValue;
            if (dr.Table.Columns.Contains("NominationStatus")) retValue.NominationStatus = dr["NominationStatus"] != System.DBNull.Value ? (EventStatus)Enum.Parse(typeof(EventStatus), Convert.ToString(dr["NominationStatus"]), true) : EventStatus.Closed;
            if (dr.Table.Columns.Contains("SponsorStartDate")) retValue.SponsorStartDate = dr["SponsorStartDate"] != System.DBNull.Value ? Convert.ToDateTime(dr["SponsorStartDate"]) : DateTime.MinValue;
            if (dr.Table.Columns.Contains("SponsorStatus")) retValue.SponsorStatus = dr["SponsorStatus"] != System.DBNull.Value ? (EventStatus)Enum.Parse(typeof(EventStatus), Convert.ToString(dr["SponsorStatus"]), true) : EventStatus.Closed;
            retValue.Organiser = new User();
            if (dr.Table.Columns.Contains("Organiser"))
                retValue.Organiser.UserID = dr["Organiser"].ToString();
            if (dr.Table.Columns.Contains("FName"))
                retValue.Organiser.FName = dr["FName"].ToString();
            if (dr.Table.Columns.Contains("LName"))
                retValue.Organiser.LName = dr["LName"].ToString();
            if (dr.Table.Columns.Contains("VoterTrack")) retValue.VoterTrack = dr["VoterTrack"] != System.DBNull.Value ? (Track)Enum.Parse(typeof(Track), Convert.ToString(dr["VoterTrack"]), true) : Track.None;
            if (dr.Table.Columns.Contains("NominationTrack")) retValue.NominationTrack = dr["NominationTrack"] != System.DBNull.Value ? (Track)Enum.Parse(typeof(Track), Convert.ToString(dr["NominationTrack"]), true) : Track.None;
            if (dr.Table.Columns.Contains("MaxNominationCount")) retValue.MaxNominationCount = dr["MaxNominationCount"] != System.DBNull.Value ? Convert.ToInt32(dr["MaxNominationCount"]) : 0;
            if (dr.Table.Columns.Contains("AllowFileUpload")) retValue.AllowFileUpload = dr["AllowFileUpload"] != System.DBNull.Value ? Convert.ToBoolean(dr["AllowFileUpload"]) : false;
            if (dr.Table.Columns.Contains("AllowNomination")) retValue.AllowNomination = dr["AllowNomination"] != System.DBNull.Value ? Convert.ToBoolean(dr["AllowNomination"]) : false;
            if (dr.Table.Columns.Contains("AllowSponsorship")) retValue.AllowSponsorship = dr["AllowSponsorship"] != System.DBNull.Value ? Convert.ToBoolean(dr["AllowSponsorship"]) : false;
            if (dr.Table.Columns.Contains("MinSponsorAmt")) retValue.MinSponsorAmt = dr["MinSponsorAmt"] != System.DBNull.Value ? Convert.ToInt32(dr["MinSponsorAmt"]) : 0;
            if (dr.Table.Columns.Contains("VoteStartDate")) retValue.VoteStartDate = dr["VoteStartDate"] != System.DBNull.Value ? Convert.ToDateTime(dr["VoteStartDate"]) : DateTime.MinValue;
            if (dr.Table.Columns.Contains("SkinId")) retValue.SkinId = dr["SkinId"] != System.DBNull.Value ? Convert.ToInt32(dr["SkinId"]) : 0;
            if (dr.Table.Columns.Contains("MaxVoteCount")) retValue.MaxVoteCount = dr["MaxVoteCount"] != System.DBNull.Value ? Convert.ToInt32(dr["MaxVoteCount"]) : 0;
            if (dr.Table.Columns.Contains("MandateField")) retValue.MandateField = dr["MandateField"] != System.DBNull.Value ? (EventMandate)Enum.Parse(typeof(EventMandate), Convert.ToString(dr["MandateField"]), true) : EventMandate.None;
            return retValue;
        }
        public static Event[] GetPopularTopNEvent(int n, int catId, int entryCount, bool includeSuspended, string viewId)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Count", n);
            pars.Add("CatId", catId);
            ReaderResponse rr = Execute.SPReader("Voice_GetPopularEvents", pars, null);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                Event[] retValue = new Event[rr.Table.Rows.Count];
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    Event ev = SetEvent(rr.Table.Rows[i]);
                    if (ev != null)
                    {
                        int total = 0;
                        ev.Entries = Event.GetEntriesWithinRange(ev.Id, 1, entryCount, includeSuspended, viewId, ref total);
                        retValue.SetValue(ev, i);
                    }
                }
                return retValue;
            }
            else
                return null;
        }
        public static Event[] GetLatestTopNEvent(int n, int catId)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Count", n);
            pars.Add("CatId", catId);
            ReaderResponse rr = Execute.SPReader("Voice_GetLatestEvents", pars, null);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                Event[] retValue = new Event[rr.Table.Rows.Count];
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    Event ev = SetEvent(rr.Table.Rows[i]);
                    if (ev != null)
                    {
                        //int total = 0;
                        //ev.Entries = Event.GetEntriesWithinRange(ev.Id, 0, entryCount, ref total);
                        retValue.SetValue(ev, i);
                    }
                }
                return retValue;
            }
            else
                return null;
        }
        public static Event[] GetTopNLatestResults(int n, int catId, int entryCount)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Count", n);
            pars.Add("CatId", catId);
            ReaderResponse rr = Execute.SPReader("Voice_GetLatestResults", pars, null);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                return SetEventWinner(rr.Table.Rows);
            }
            else
                return null;
        }
        private static Event[] SetEventWinner(DataRowCollection drs)
        {
            ArrayList listId = new ArrayList();
            ArrayList evList = new ArrayList();
            ArrayList wList = new ArrayList();

            Event ev = null;
            for (int i = 0; i < drs.Count; i++)
            {
                DataRow dr = drs[i];
                int evId = 0;
                if (dr.Table.Columns.Contains("EventId")) evId = dr["EventId"] != null ? Convert.ToInt32(dr["EventId"]) : 0;
                if (!listId.Contains(evId))
                {
                    if (ev != null)
                    {
                        ev.Winners = new EventWinner[wList.Count];
                        for (int j = 0; j < wList.Count; j++)
                        {
                            ev.Winners.SetValue(wList[j], j);
                        }
                        evList.Add(ev);
                    }
                    ev = new Event();
                    ev.Id = evId;
                    listId.Add(evId);
                    if (dr.Table.Columns.Contains("EventName")) ev.Name = dr["EventName"] != null ? Convert.ToString(dr["EventName"]) : "";
                }
                int position = 0;
                if (dr.Table.Columns.Contains("Position")) position = dr["Position"] != null ? Convert.ToInt32(dr["Position"]) : 0;
                Entry en = new Entry();
                if (dr.Table.Columns.Contains("EntryId")) en.Id = dr["EntryId"] != null ? Convert.ToInt32(dr["EntryId"]) : 0;
                if (dr.Table.Columns.Contains("EntryTitle")) en.Title = dr["EntryTitle"] != null ? Convert.ToString(dr["EntryTitle"]) : "";
                if (dr.Table.Columns.Contains("Votes")) en.Votes = dr["Votes"] != null ? Convert.ToInt32(dr["Votes"]) : 0;
                if (dr.Table.Columns.Contains("Points")) en.Points = dr["Points"] != null ? Convert.ToInt32(dr["Points"]) : 0;
                en.Upload = new File();
                if (dr.Table.Columns.Contains("Location"))
                    en.Upload.Location = dr["Location"].ToString();
                if (dr.Table.Columns.Contains("Text"))
                    en.Upload.Text = dr["Text"].ToString();
                if (dr.Table.Columns.Contains("FileType"))
                    en.Upload.Type = File.GetFileType(dr["FileType"].ToString());
                if (dr.Table.Columns.Contains("Width"))
                    en.Upload.Width = dr["Width"] != null ? Convert.ToInt32(dr["Width"]) : 0;
                if (dr.Table.Columns.Contains("Height"))
                    en.Upload.Height = dr["Height"] != null ? Convert.ToInt32(dr["Height"]) : 0;
                en.Nominee = new User();
                if (dr.Table.Columns.Contains("Nominee"))
                    en.Nominee.UserID = dr["Nominee"].ToString();
                if (dr.Table.Columns.Contains("FName"))
                    en.Nominee.FName = dr["FName"].ToString();
                if (dr.Table.Columns.Contains("LName"))
                    en.Nominee.LName = dr["LName"].ToString();

                EventWinner ew = new EventWinner(position, en);
                wList.Add(ew);
                if (ev != null && i == drs.Count - 1)
                {
                    ev.Winners = new EventWinner[wList.Count];
                    for (int j = 0; j < wList.Count; j++)
                    {
                        ev.Winners.SetValue(wList[j], j);
                    }
                    evList.Add(ev);
                }
            }
            Event[] retValue = new Event[evList.Count];
            for (int k = 0; k < evList.Count; k++)
            {
                retValue.SetValue(evList[k], k);
            }
            return retValue;
        }
        public static Event[] Search(int catId, string key, int startIndex, int rowcount, string orderby, ref int total)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("catId", catId);
            pars.Add("key", key);
            pars.Add("startIndex", startIndex);
            pars.Add("rowCount", rowcount);
            pars.Add("orderBy", orderby);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("count", new VSqlDbType(SqlDbType.Int, -1));
            ReaderResponse rr = Execute.SPReader("Voice_SearchEvent", pars, outpars);
            total = Convert.ToInt32(rr.OutParams["count"]);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                Event[] retValue = new Event[rr.Table.Rows.Count];
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    retValue.SetValue(SetEvent(rr.Table.Rows[i]), i);
                }
                return retValue;
            }
            else
                return null;
        }
        public static Event[] GetUserEvents(string userId, int count)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("UserId", userId);
            pars.Add("Count", count);
            ReaderResponse rr = Execute.SPReader("Voice_GetUserEvents", pars, null);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                Event[] retValue = new Event[rr.Table.Rows.Count];
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    retValue.SetValue(SetEvent(rr.Table.Rows[i]), i);
                }
                return retValue;
            }
            else
                return null;
        }
        #region DML
        static Dictionary<string, object> GetParameters(Event e)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Name", e.Name);
            pars.Add("Description", e.Description);
            pars.Add("CatagoryId", e.CatagoryId);
            pars.Add("Count", 0);
            pars.Add("Votes", e.Votes);
            pars.Add("Status", Convert.ToString(e.StartDate.CompareTo(DateTime.Today) > 0 ? EventStatus.YetToStart : EventStatus.Started));
            pars.Add("EventStartDate", (DateTime)e.StartDate);
            pars.Add("EventEndDate", (DateTime)e.EndDate);
            pars.Add("FeeAmount", e.FeeAmount);
            pars.Add("Currency", e.Currency.ToString());
            pars.Add("Access", e.Access.ToString());
            pars.Add("AccessPublic", e.AccessPublic.ToString());
            pars.Add("AccessCode", e.AccessCode);
            pars.Add("NominationStartDate", (DateTime)e.NominationStartDate);
            pars.Add("NominationStatus", Convert.ToString(e.NominationStartDate.CompareTo(DateTime.Today) > 0 ? EventStatus.YetToStart : EventStatus.Started));
            pars.Add("SponsorStartDate", (DateTime)e.SponsorStartDate);
            pars.Add("SponsorStatus", Convert.ToString(e.SponsorStartDate.CompareTo(DateTime.Today) > 0 ? EventStatus.YetToStart : EventStatus.Started));
            pars.Add("Organiser", e.Organiser.UserID);
            pars.Add("VoterTrack", e.VoterTrack.ToString());
            pars.Add("NominationTrack", e.NominationTrack.ToString());
            pars.Add("MaxNominationCount", e.MaxNominationCount);
            pars.Add("AllowFileUpload", e.AllowFileUpload);
            pars.Add("AllowNomination", e.AllowNomination);
            pars.Add("AllowSponsorship", e.AllowSponsorship);
            pars.Add("MinSponsorAmt", e.MinSponsorAmt);
            pars.Add("VoteStartDate", (DateTime)e.VoteStartDate);
            pars.Add("SkinId", e.SkinId);
            pars.Add("MaxVoteCount", e.MaxVoteCount);
            pars.Add("MandateField", e.MandateField.ToString());
            string criteria = "<root>";
            foreach (string key in e.Criteria.Keys)
            {
                criteria += "<c k=\"" + key + "\" v=\"" + e.Criteria[key] + "\" />";
            }
            criteria += "</root>";
            pars.Add("Criteria", criteria);
            string fTypes = "";
            if (e.UploadFileTypes != null)
            {
                foreach (EventFileType eType in e.UploadFileTypes)
                {
                    fTypes += eType.ToString() + ",";
                }
            }
            pars.Add("FileTypes", fTypes);
            return pars;
        }
        public static EventResponse Create(Event e)
        {
            Dictionary<string, object> pars = GetParameters(e);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("EventId", new VSqlDbType(SqlDbType.Int));
            outpars.Add("Result", new VSqlDbType(SqlDbType.VarChar, 20));
            NonQueryResponse nqr = Execute.SPNonQuery("Voice_CreateEvent", pars, outpars);
            string res = nqr.OutParams["Result"].ToString();
            e.Id = Convert.ToInt32(nqr.OutParams["EventId"]);
            switch (res.ToLower())
            {
                case "eventcreated":
                    return EventResponse.EventCreated;
                default:
                    return EventResponse.Error;
            }
        }
        public static EventResponse Update(Event e)
        {
            Dictionary<string, object> pars = GetParameters(e);
            pars.Add("EventId", e.Id);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("Result", new VSqlDbType(SqlDbType.VarChar, 20));
            NonQueryResponse nqr = Execute.SPNonQuery("Voice_UpdateEvent", pars, outpars);
            string res = nqr.OutParams["Result"].ToString();
            switch (res.ToLower())
            {
                case "eventupdated":
                    return EventResponse.EventUpdated;
                default:
                    return EventResponse.Error;
            }
        }

        #endregion
    }
}
